package TrueTopic;

import java.util.HashMap;
import java.util.Scanner;

public class TestG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n1 = sc.nextInt();
            StringBuilder sb1 = new StringBuilder();
            for (int i = 0; i < n1; i++) {
                String s = sc.next();
                int sum = sc.nextInt();
                for (int j = 0; j < sum; j++) {
                    sb1.append(s);
                }
            }
            n1 = sc.nextInt();
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < n1; i++) {
                String s = sc.next();
                int sum = sc.nextInt();
                for (int j = 0; j < sum; j++) {
                    sb2.append(s);
                }
            }
            if (sb1.length()>sb2.length()){
                System.out.println("no");
            }else {
                int mark = 0;
                for (int i = 0; i < sb1.length(); i++) {
                    for (int j = 0; j < sb2.length(); j++) {
                        if (sb1.charAt(i) == sb2.charAt(j)) {
                            sb1.delete(0, i + 1);
                            sb2.delete(0, j + 1);
                        }
                        if (sb1.length() == 0) {
                            mark = 1;
                            break;
                        }
                    }
                }
                if (mark == 0) {
                    System.out.println("no");
                } else {
                    System.out.println("yes");
                }
            }
        }
    }
}
